//
//  FFTools.m
//  Common
//
//  Created by IT.Bear on 16/5/20.
//  Copyright (c) 2016年 Fashionfinger. All rights reserved.
//

#import "FFTools.h"
#import "FFMacro.h"
#import "SDWebImageManager.h"

static NSDateFormatter* dateFormaterObj = nil;

@implementation FFTools

+(CGFloat)imageHeight:(NSString *)imageUrl
{
    NSURL * url = [NSURL URLWithString:imageUrl];
    
    //    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    BOOL existBool = [manager diskImageExistsForURL:url];//判断是否有缓存
    
    UIImage * image;
    
    if (existBool) {
        
        image = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
        
    }else{
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        image = [UIImage imageWithData:data];
        
    }
    //根据image的比例来设置高度
    
    CGFloat defaultWidth = IPHONE_WIDTH;
    CGFloat imageHeight;
    if (image.size.width) {
        
        imageHeight = image.size.height / image.size.width * defaultWidth;
        
    }
    return imageHeight;
}

//去除float多余的0
+(NSString*)removeFloatAllZero:(NSString*)string
{
    NSString * testNumber = [NSString stringWithFormat:@"%.0f",([string floatValue])];
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}

// 将JSON串转化为字典或者数组
+ (id)toArrayOrNSDictionary:(NSString *)urlStr{
    NSError *error = nil;
    NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
    id info;
    if (data) {
        info = [NSJSONSerialization
                JSONObjectWithData:data
                options:kNilOptions
                error:&error];
    }
    if ([info isKindOfClass:[NSArray class]]){
        return (NSArray *)info;
    }else if ([info isKindOfClass:[NSDictionary class]]){
        return (NSDictionary *)info;
    }else{
        // 解析错误
        return nil;
    }
}

//通过图片Data数据第一个字节 来获取图片扩展名
+ (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];
    switch (c) {
        case 0xFF:
            return @"jpeg";
        case 0x89:
            return @"png";
        case 0x47:
            return @"gif";
        case 0x49:
        case 0x4D:
            return @"tiff";
        case 0x52:
            if ([data length] < 12) {
                return nil;
            }
            NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
            if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                return @"webp";
            }
            return nil;
    }
    return nil;
}

+(NSArray *)IsContainChinese:(NSString *)str
{
    NSMutableArray *array = [NSMutableArray array];
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff){
            [array addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return array;
}

+ (UIImage *)makeImageWithView:(UIView *)view

{
    
    CGSize s = view.bounds.size;
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数。
    
    UIGraphicsBeginImageContextWithOptions(s, YES, [UIScreen mainScreen].scale);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}

+ (NSDictionary *)jsonStringToDic:(NSString*)jsonString
{
    jsonString = [jsonString stringByRemovingPercentEncoding];
    NSError *error;
    NSDictionary *info = [NSJSONSerialization
                          JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
                          options:kNilOptions
                          error:&error];
    if([info count] > 0){
        return info;
    }
    return nil;
}

+ (UIView*)duplicate:(UIView*)view
{
    NSData * tempArchive = [NSKeyedArchiver archivedDataWithRootObject:view];
    return [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
}

+ (NSString *)verticalString:(NSString *)content
{
    if (!content) {
        return @"";
    }
    NSMutableString * str = [[NSMutableString alloc] initWithString:content];
    NSInteger count = str.length;
    for (int i = 1; i < count; i ++) {
        [str insertString:@"\n" atIndex:i*2 - 1];
    }
    return str;
}

+ (NSDate *)dateFromString:(NSString *)timeStr
                    format:(NSString *)format
{
    NSDateFormatter *dateFormatter = [FFTools dateFormatter];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    return date;
}

+ (NSInteger)cTimestampFromDate:(NSDate *)date
{
    return (long)[date timeIntervalSince1970];
}


+(NSInteger)cTimestampFromString:(NSString *)timeStr
                          format:(NSString *)format
{
    NSDate *date = [FFTools dateFromString:timeStr format:format];
    return [FFTools cTimestampFromDate:date];
}

+ (NSString *)dateStrFromCstampTime:(NSInteger)timeStamp
                     withDateFormat:(NSString *)format
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [FFTools datestrFromDate:date withDateFormat:format];
}

+ (NSString *)datestrFromDate:(NSDate *)date
               withDateFormat:(NSString *)format{
    NSDateFormatter *dateToStringFormatter=[[NSDateFormatter alloc] init];
    [dateToStringFormatter setDateFormat:format];
    NSString *nsDate=[dateToStringFormatter stringFromDate:date];
    //NSLog(@"格式化指定日期:%@", nsDate);
    return nsDate;
}

+ (NSString *)getWeek:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    now=date;
    comps = [calendar components:unitFlags fromDate:now];
    //    int year=[comps year];
    //    int week = [comps weekday];
    //    int month = [comps month];
    //    int day = [comps day];
    //    int hour = [comps hour];
    //    int min = [comps minute];
    //    int sec = [comps second];
    
    NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    
    NSString *weakStr = [NSString stringWithFormat:@"%@",[arrWeek objectAtIndex:[comps weekday] - 1]];
    return weakStr;
}

/**
 *  时间戳时间字符串
 *
 *  @param timeStamp 时间戳
 *
 *  @return 返回字符串格式时间
 */
+ (NSString *)getTimeWithTimeStamp:(long long)timeStamp{
    NSString * hour = [NSString stringWithFormat:@"%ld",(long)timeStamp / (60 * 60)];
    NSString * minute = [NSString stringWithFormat:@"%ld",(long)(timeStamp % (60 * 60 )) / 60];
    NSString * seconds = [NSString stringWithFormat:@"%ld",(long)(timeStamp % (60 * 60 )) % 60];
    
    NSString *showHour;
    NSString *showMin;
    NSString *showSec;
    NSString *showDay;
    if ([hour length]>=3) {
        showHour = [NSString stringWithFormat:@"%@",[hour substringWithRange:NSMakeRange(0, [hour length])]];
    }else if ([hour length]==2) {
        
        showHour = [NSString stringWithFormat:@"%@",hour];
    }else if ([hour length]==1){
        
        showHour = [NSString stringWithFormat:@"0%@",hour];
    }
    
    if ([minute length]==2) {
        showMin = [NSString stringWithFormat:@"%@",[minute substringWithRange:NSMakeRange(0, 2)]];
    }else if ([minute length]==1){
        showMin = [NSString stringWithFormat:@"0%@",minute];
    }
    
    if ([seconds length]==2) {
        showSec = [seconds substringWithRange:NSMakeRange(0, 2)];
    }else if ([seconds length]==1){
        showSec = [NSString stringWithFormat:@"0%@",seconds];
    }
    
    if ([hour integerValue]) {
        if ([hour integerValue]>24) {
            showDay = [NSString stringWithFormat:@"%02ld",[hour integerValue]/24];
            showHour = [NSString stringWithFormat:@"%02ld",[hour integerValue]%24];
            return [NSString stringWithFormat:@"%@:%@:%@:%@",showDay,showHour,showMin,showSec];
        }
        else{
            return [NSString stringWithFormat:@"%@:%@:%@",showHour,showMin,showSec];
        }
    }
    else{
        return [NSString stringWithFormat:@"%@:%@",showMin,showSec];
    }
}

+ (BOOL)checkStringIsBlank:(NSString*)str{
    BOOL isBlank = YES;
    if ([str isKindOfClass:[NSNull class]]) {
        str = nil;
    }
    if (str == nil) {
        isBlank = YES;
    }
    for (int i = 0; i < str.length; i++) {
        NSRange range;
        range.location = i; range.length = 1;
        NSString* _s = [str substringWithRange:range];
        if (![_s isEqualToString:@" "]) {
            isBlank = NO;
            break;
        }
    }
    return isBlank;
}

+ (NSDateFormatter*)dateFormatter{
    if (dateFormaterObj == nil) {
        dateFormaterObj = [[NSDateFormatter alloc] init];
    }
    return dateFormaterObj;
}

//format == yyyy-MM-dd HH:mm:ss
+ (NSString*)timeToString:(double)unixtime Format:(NSString*)formatStr{
    NSString *timeStr = @"";
    if (unixtime > 0) {
        NSDate* date = [NSDate dateWithTimeIntervalSince1970:unixtime];
        NSDateFormatter *dateForMatter =[self dateFormatter];
        dateForMatter.timeZone = [NSTimeZone systemTimeZone];
        [dateForMatter setDateFormat:formatStr];
        timeStr = [dateForMatter stringFromDate:date];
    }
    return timeStr;
}

+ (BOOL)validateChinese:(NSString *)str{
    NSString *regex = @"^[\u4E00-\u9FA5]*$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [emailTest evaluateWithObject:str];
}

+ (BOOL)validateIdentityCard:(NSString *)identityCard{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

+ (UIColor *)randomColor{
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

+ (UITableViewCell *)tableViewCell:(id)obj{
    UITableViewCell *cell = (UITableViewCell*)[obj superview];
    if ([cell isKindOfClass:[UITableViewCell class]]) {
        return cell;
    }else{
        return [self tableViewCell:cell];
    }
}

+(BOOL)MatchLetter:(NSString *)str
{
    //判断是否以字母开头
    NSString *ZIMU = @"^[A-Za-z]+$";
    NSPredicate *regextestA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ZIMU];
    
    if ([regextestA evaluateWithObject:str] == YES)
        return YES;
    else
        return NO;
}

+ (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


//判断内容是否全部为空格  yes 全部为空格  no 不是
+ (BOOL)isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

+ (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
+ (UIImage*) GetImageWithColor:(UIColor*)color andSize:(CGSize)size
{
    CGRect r= CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

@end

@implementation UIImage (CS_Extensions)
- (UIImage *)imageAtRect:(CGRect)rect {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage* subImage = [UIImage imageWithCGImage: imageRef];
    CGImageRelease(imageRef);
    return subImage;
}

- (UIImage *)imageByScalingToSize:(CGSize)targetSize{
    
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    UIGraphicsBeginImageContext(targetSize);
    
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if(newImage == nil) NSLog(@"could not scale image");
    return newImage ;
}

-(UIImage*)reSizeImage:(CGSize)maxSize{
    CGSize originSize = self.size;
    CGSize newSize = originSize;
    if (originSize.width > originSize.height) {
        //宽图，只要考虑最大宽度即可
        if (originSize.width > maxSize.width) {
            newSize.width = maxSize.width;
            newSize.height = originSize.height*(newSize.width/originSize.width);
        }
        
    }else{
        //考虑最大高度即可
        if (originSize.height > maxSize.height) {
            newSize.height = maxSize.height;
            newSize.width = originSize.width*(newSize.height/originSize.height);
        }
    }
    return  [self imageByScalingToSize:newSize];
}
@end

@implementation UIButton (ExtentionButton)
+ (UIButton*)buttonWithMyImage:(NSString*)imageName{
    UIImage* btnImg = [UIImage imageNamed:imageName];
    CGSize imgSize = btnImg.size;
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:btnImg forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    return btn;
}

+ (UIButton*)buttonWithMyImage:(NSString*)imageName stretchInset:(UIEdgeInsets)inset{
    UIImage* btnImg = [UIImage imageNamed:imageName];
    UIImage* btnImg2 = [btnImg resizableImageWithCapInsets:inset];
    CGSize imgSize = btnImg.size;
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:btnImg2 forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    return btn;
}

+ (UIButton*)buttonWithMyImage:(NSString *)imageName Frame:(CGRect)rect{
    UIImageView* imgView = [UIImageView imageViewWithMyImage:imageName];
    [imgView setCenter:CGPointMake(rect.size.width/2.0, rect.size.height/2.0)];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:rect];
    
    [btn addSubview:imgView];
    
    return btn;
}

+ (UIButton*)buttonWithTitle:(NSString*)title color:(UIColor*)_color{
    CGRect rect = CGRectMake(0, 0, 55, 31);
    return [self buttonWithTitle:title color:_color rect:rect];
}

+ (UIButton*)buttonWithTitle:(NSString*)title color:(UIColor*)_color rect:(CGRect)_rect{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:_rect];
    [btn setClipsToBounds:YES];
    [btn setBackgroundColor:_color];
    [btn setBackgroundImage:[UIImage imageNamed:@"mask_link"] forState:UIControlStateHighlighted];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn.titleLabel setFont:APP_FONT(14)];
    btn.layer.cornerRadius = _rect.size.height/2.0;
    return btn;
}

@end

@implementation UIImageView(ExtentionImageView)
+ (UIImageView*)imageViewWithMyImage:(NSString*)imageName{
    UIImage* img = [UIImage imageNamed:imageName];
    CGSize imgSize = img.size;
    UIImageView* imageView = [[UIImageView alloc] initWithImage:img];
    [imageView setFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    return imageView;
}
//圆角矩阵拉伸
+ (UIImageView*)imageViewWithMyImage:(NSString*)imageName stretchInset:(UIEdgeInsets)inset{
    UIImage* img = [UIImage imageNamed:imageName];
    UIImage* img2 = [img resizableImageWithCapInsets:inset];
    CGSize imgSize = img2.size;
    UIImageView* imageView = [[UIImageView alloc] initWithImage:img2];
    [imageView setFrame:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    return imageView;
}

@end

@implementation UITextField(ExtentionTextField)
- (void)setLeftMargin:(float)width{
    self.leftViewMode = UITextFieldViewModeAlways;
    [self setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 0)]];
}
@end

@implementation NSObject (SuperFaultTolerant)

//- (id)ARRAY_OBJ_AT{
//    return nil;
//}
//- (void)ARRAY_ADD_OBJ{
//
//}
//- (void)ARRAY_ADD_OBJS_FROM_ARRAY{
//
//}
//- (void)ARRAY_INSERT_OBJ_AT{
//
//}
//- (void)ARRAY_REMOVEOBJ_AT{
//
//}
//- (void)ARRAY_REMOVE_OBJ{
//
//}
//- (id)DICT_OBJ_FOR_K{
//    return nil;
//}
//- (void)DICT_SET_VK{
//
//}
//- (void)DICT_REMOVEOBJ_K{
//
//}
//- (void)DICT_SET_DICT{
//
//}
//- (BOOL)DICT_HAS_K{
//    return NO;
//}
//- (void)DICT_REMOVEOBJS_KS{
//
//}



-(void)My_removeObjectAtIndex:(NSInteger)index File:(char*)file Line:(int)line{ //NSMutableArray
    if ([self isKindOfClass:[NSMutableArray class]]) {
        if (index >= 0 && index < [(NSArray*)self count]) {
            [(NSMutableArray*)self removeObjectAtIndex:index];
        }else{
            NSLog(@"[error array:%@ index:%ld 越界 count:%d | 在文件:%s  第:%d 行]",self,(long)index,(int)[(NSArray*)self count],file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(void)My_removeAllObjects_File:(char*)file Line:(int)line{ //NSMutableArray
    if ([self isKindOfClass:[NSMutableArray class]] || [self isKindOfClass:[NSMutableDictionary class]]) {
        [(NSMutableArray*)self removeAllObjects];
    }else{
        NSLog(@"this is %@ class not NSMutableArray or NSMutableDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(void)My_removeObject:(id)obj File:(char*)file Line:(int)line{ //NSMutableArray
    if ([self isKindOfClass:[NSMutableArray class]]) {
        if (obj != nil) {
            [(NSMutableArray*)self removeObject:obj];
        }else{
            NSLog(@"删除对象不要为nil | 在文件:%s  第:%d 行]",file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(id)My_objectAtIndex:(NSInteger)index File:(char*)file Line:(int)line{ //NSArray
    id obj = nil;
    if ([self isKindOfClass:[NSArray class]]) {
        if (index >= 0 && index < [(NSArray*)self count]) {
            obj = [(NSArray*)self objectAtIndex:index];
        }else{
            if (index > 0) {
                NSLog(@"[error array:%@ index:%ld 越界 count:%d | 在文件:%s  第:%d 行]",self,(long)index,(int)[(NSArray*)self count],file,line);
            }
            
        }
    }else{
        NSLog(@"this is %@ class not NSArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
    
    return obj;
}
-(NSString*)MY_joinByString:(NSString*)sep File:(char*)file Line:(int)line{
    NSString* joined = nil;
    if ([self isKindOfClass:[NSArray class]]) {
        if ([sep isKindOfClass:[NSString class]]) {
            joined = [(NSArray*)self componentsJoinedByString:sep];
        }else{
            NSLog(@"this is %@ class not NSString | 在文件:%s  第:%d 行",[sep class],file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return joined;
}
-(void)My_setObject:(id)value forKey:(id)key File:(char*)file Line:(int)line{ //NSMutableDictionary
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
        if (key == nil) {
            NSLog(@"key 不能为空 在文件:%s  第:%d 行]",file,line);
            return;
        }
        if (value == nil) {
            NSLog(@"value 不能为空 在文件:%s  第:%d 行]",file,line);
            return;
        }
        [(NSMutableDictionary*)self setObject:value forKey:key];
    }else{
        NSLog(@"this is %@ class not NSMutableDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(void)My_setDict:(NSDictionary*)dict File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [(NSMutableDictionary*)self setDictionary:dict];
        }else{
            NSLog(@"dict is %@ class not NSDictionary | 在文件:%s  第:%d 行",[dict class],file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(void)My_removeObjectForKey:(id)key File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
        if (key != nil) {
            [(NSMutableDictionary*)self removeObjectForKey:key];
        }else{
            NSLog(@"key is 不能为nil | 在文件:%s  第:%d 行",file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(void)My_removeObjectsForKeys:(NSArray*)keys File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
        if ([keys isKindOfClass:[NSArray class]]) {
            [(NSMutableDictionary*)self removeObjectsForKeys:keys];
        }else{
            NSLog(@"keys is %@ class not NSArray | 在文件:%s  第:%d 行",[keys class],file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
}

-(BOOL)My_hasKey:(id)key File:(char*)file Line:(int)line{
    BOOL has = NO;
    if ([self isKindOfClass:[NSDictionary class]]) {
        if (key != nil) {
            if ([(NSDictionary*)self objectForKey:key] != nil) {
                has = YES;
            }
            
        }else{
            NSLog(@"aKey is nil | 在文件:%s  第:%d 行",file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return has;
}

- (int)My_count_File:(char*)file Line:(int)line{
    int counti = 0;
    if ([self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSArray class]]) {
        counti = (int)[(NSArray*)self count];
    }else{
        NSLog(@"this is %@ class not NSDictionary or NSArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return counti;
}
- (id)My_objectForKey:(id)aKey File:(char*)file Line:(int)line{
    id obj = nil;
    if ([self isKindOfClass:[NSDictionary class]]) {
        if (aKey != nil) {
            obj = [(NSDictionary*)self objectForKey:aKey];
        }else{
            NSLog(@"aKey is nil | 在文件:%s  第:%d 行",file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return obj;
}
- (NSArray *)My_allKeys_File:(char*)file Line:(int)line{
    NSArray* allKeys = nil;
    if ([self isKindOfClass:[NSDictionary class]]) {
        allKeys = [(NSDictionary*)self allKeys];
    }else{
        NSLog(@"this is %@ class not NSDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return allKeys;
}
- (NSArray *)My_allValues_File:(char*)file Line:(int)line{
    NSArray* allValues = nil;
    if ([self isKindOfClass:[NSDictionary class]]) {
        allValues = [(NSDictionary*)self allValues];
    }else{
        NSLog(@"this is %@ class not NSDictionary | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return allValues;
}
- (void)My_addObject:(id)anObject File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableArray class]]) {
        if (anObject != nil) {
            [(NSMutableArray*)self addObject:anObject];
        }else{
            NSLog(@"anObject is nil | 在文件:%s  第:%d 行",file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
}

- (void)My_addObjectsFromArray:(NSArray*)objs File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableArray class]]) {
        if ([objs isKindOfClass:[NSArray class]]) {
            [(NSMutableArray*)self addObjectsFromArray:objs];
        }else{
            NSLog(@"objs is not NSArray | 在文件:%s  第:%d 行",file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
}

- (void)My_insertObject:(id)anObject atIndex:(NSInteger)index File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableArray class]]) {
        if (anObject == nil) {
            NSLog(@"anObject is nil | 在文件:%s  第:%d 行",file,line);
            return;
        }
        if (index >= 0 && index <= [(NSMutableArray*)self count]) {
            [(NSMutableArray*)self insertObject:anObject atIndex:index];
        }else{
            NSLog(@"[insert array:%@ index:%ld 越界 count:%d | 在文件:%s  第:%d 行]",self,(long)index,(int)[(NSArray*)self count],file,line);
        }
    }else{
        NSLog(@"this is %@ class not NSMutableArray | 在文件:%s  第:%d 行",[self class],file,line);
    }
}
-(int)My_length_File:(char*)file Line:(int)line{
    int lengthI = 0;
    if ([self isKindOfClass:[NSString class]] || [self isKindOfClass:[NSData class]]) {
        lengthI = (int)[(NSString*)self length];
    }else{
        NSLog(@"this is %@ class not NSString or NSData | 在文件:%s  第:%d 行",[self class],file,line);
    }
    return lengthI;
}

-(void)My_setString:(NSString*)str File:(char*)file Line:(int)line{
    if ([self isKindOfClass:[NSMutableString class]]) {
        if (str != nil) {
            [(NSMutableString*)self setString:str];
        }else{
            NSLog(@"anObject is nil | 在文件:%s  第:%d 行",file,line);
            return;
        }
    }
}

-(void)performBlock:(void (^)(void))block Delay:(float)delay{
    [self performSelector:@selector(fireBlockAfterDelay:) withObject:block afterDelay:delay];
}

-(void)fireBlockAfterDelay:(void (^)(void))block
{
    block();
}

+ (NSString *)dataToJsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

+ (NSString *)getTimesp
{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    //    时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}

+ (NSString*)replaceUnicode:(NSString*)aUnicodeString

{
    
    NSString *tempStr1 = [aUnicodeString stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
    
}
@end


NSString *NSStringFromOBJPointer(id obj){
    return [NSString stringWithFormat:@"%d",(int)obj];
}

NSString *NSStringFromInt(NSInteger num){
    return [NSString stringWithFormat:@"%ld",(long)num];
}

NSString *NSStringFromFloat(float num){
    return [NSString stringWithFormat:@"%f",num];
}

NSString *NSStringFromNumber(id obj){
    if ([obj isKindOfClass:[NSString class]]) {
        return obj;
    }
    return [NSString stringWithFormat:@"%@",obj];
}

